package com.humanresource.service.impl.clerkrecord;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.humanresource.dao.mapper.clerkrecord.PosMapper;
import com.humanresource.dao.mapper.clerksalary.PossalarystandardMapper;
import com.humanresource.domain.po.clerkrecord.Pos;
import com.humanresource.domain.po.clerkrecord.PosExample;
import com.humanresource.domain.po.clerksalary.Possalarystandard;
import com.humanresource.domain.po.clerksalary.Salarystandard;
import com.humanresource.service.clerkrecord.PosService;
import com.humanresource.service.clerksalary.SalarystandardService;

@Service
public class PosServiceImpl implements PosService {

	@Autowired
	private PosMapper posMapper;

	@Autowired
	private PossalarystandardMapper possalarystandardMapper;

	@Autowired
	private SalarystandardService salarystandardService;

	@Override
	public Pos findById(Integer posId) {
		return this.posMapper.selectByPrimaryKey(posId);
	}

	@Override
	public Pos findByName(String posName) {
		PosExample posExample = new PosExample();
		PosExample.Criteria criteria = posExample.createCriteria();
		criteria.andPosnameEqualTo(posName);
		return this.posMapper.selectByExample(posExample).get(0);
	}

	@Override
	public Possalarystandard findByPosId(Integer posId) {
		return this.possalarystandardMapper.selectByPrimaryKey(posId);
	}

	@Override
	public Map<String, List<Pos>> posList() {
		Map<String, List<Pos>> posMap = new HashMap<String, List<Pos>>();
		PosExample posExample1 = new PosExample();
		PosExample.Criteria criteria1 = posExample1.createCriteria();
		criteria1.andPospidEqualTo(0);
		List<Pos> pos1List = posMapper.selectByExample(posExample1);
		posMap.put("职位分类", pos1List);

		List<Pos> pos2List = new ArrayList<Pos>();
		for (Pos pos : pos1List) {
			PosExample posExample2 = new PosExample();
			PosExample.Criteria criteria2 = posExample2.createCriteria();
			criteria2.andPospidEqualTo(pos.getPosid());
			List<Pos> posList = posMapper.selectByExample(posExample2);
			pos2List.addAll(posList);
		}
		posMap.put("职位名称", pos2List);

		List<Pos> pos3List = new ArrayList<Pos>();
		for (Pos pos : pos2List) {
			PosExample posExample3 = new PosExample();
			PosExample.Criteria criteria3 = posExample3.createCriteria();
			criteria3.andPospidEqualTo(pos.getPosid());
			List<Pos> posList = posMapper.selectByExample(posExample3);
			pos3List.addAll(posList);
		}
		posMap.put("职称", pos3List);
		return posMap;
	}

	@Override
	public Salarystandard findSalarystandardByPosname(String posname) {
		Pos pos = this.findByName(posname);
		Possalarystandard possalarystandard = this.findByPosId(pos.getPosid());
		return this.salarystandardService.findById(possalarystandard
				.getSalarystandardid());
	}

}
